package com.lmabbe.oauth.context;

import com.lmabbe.common.global.context.metadata.client.OauthClient;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * @author lmabbe
 */
public class MyClientDetails implements ClientDetails {

    private final OauthClient oauthClient;
    private final PasswordEncoder bCryptPasswordEncoder;

    public MyClientDetails(OauthClient oauthClient, PasswordEncoder bCryptPasswordEncoder) {
        this.oauthClient = oauthClient;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Override
    public String getClientId() {
        return oauthClient.getClientKey();
    }

    @Override
    public Set<String> getResourceIds() {
        return oauthClient.getResourceIds();
    }

    @Override
    public boolean isSecretRequired() {
        return false;
    }

    @Override
    public String getClientSecret() {
        return bCryptPasswordEncoder.encode(oauthClient.getClientSecret());
    }

    @Override
    public boolean isScoped() {
        return false;
    }

    @Override
    public Set<String> getScope() {
        return oauthClient.getScopes();
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return oauthClient.getGrantTypes();
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return Set.of(oauthClient.getRedirectUris());
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return null;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return null;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}
